package com.cheyunkeji.er.view;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.service.CountDownService;


/**
 * 验证码等待倒计时
 * @see CountDownService 倒计时服务绑定
 */
public class VerifyCodeView extends TextView implements ServiceConnection, CountDownService.OnCountChangedAction {

    // 短信倒计时后台服务
    private CountDownService countDownService;
    // 是否在倒计时状态
    private boolean isCounting = false;

    public VerifyCodeView(Context context) {
        super(context);
        init();
    }

    public VerifyCodeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VerifyCodeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public VerifyCodeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        // 绑定验证码倒计时服务
        getContext().bindService(new Intent(getContext(), CountDownService.class), this, Service.BIND_AUTO_CREATE);
    }

    public boolean isCounting() {
        return isCounting;
    }

    public void startCountDown() {
        // 开始倒计时服务
//        getContext().startService(new Intent(getContext(), CountDownService.class));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            getContext().startForegroundService(new Intent(getContext(), CountDownService.class));
        } else {
            getContext().startService(new Intent(getContext(), CountDownService.class));
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        countDownService = ((CountDownService.MyBinder) service).getService();
        countDownService.setOnCountChangedAction(this);
        isCounting = countDownService.getCurMillis() != 0;

        if (isCounting)
            this.setEnabled(false);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // no-op
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // 倒计时期间不可以点击
        this.setEnabled(false);

        // 倒计时剩余时间
        String content = String.format(getResources().getString(R.string.label_wait_auth_code), millisUntilFinished / 1000);
        SpannableStringBuilder span = new SpannableStringBuilder(content);
//                span.setSpan(new RelativeSizeSpan(0.8f), 4, content.length(), 0);
//                span.setSpan(new RelativeSizeSpan(0.8f), 0, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        this.setText(span);

        isCounting = true;
    }

    @Override
    public void onFinish() {
        // 倒计时结束了
        this.setEnabled(true);
        this.setText(R.string.label_get_auth_code_again);

        isCounting = false;

        // 清空临时保存的手机
//        AccountManager.getInstance(getContext()).saveTempPhone("");
    }

    @Override
    protected void onDetachedFromWindow() {
        // 解除倒计时服务绑定状态，不停止计数，主动停止服务
        getContext().unbindService(this);
        // onUnbind 可能还没执行到，所以这里直接设置了
//        countDownService.setOnCountChangedAction(null);

        super.onDetachedFromWindow();
    }

}
